<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算属性</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        .a {
            background-color: red;
        }

        .b {
            background-color: blue;
            color: #fff;
        }

        .c {
            background-color: yellow;
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in commodity" :key="index">{{item.name}} - {{item.price}} / <input type="text" v-model="item.count"></li>
        </ul>
        {{total}}
    </div>

    <script>
        new Vue({
            el: "#app",
            data: {
                bg: "background-color",
                commodity: [{
                        name: "陈先程",
                        price: 15,
                        count: 2,
                        checked:true
                    },
                    {
                        name: "刘沛东",
                        price: 150,
                        count: 1,
                        checked:true
                    },
                    {
                        name: "石元秋",
                        price: 1.5,
                        count: 100,
                        checked:true
                    },
                    {
                        name: "方志刚",
                        price: 1500,
                        count: 100,
                        checked:true
                    }
                ]
            },
            // 计算属性
            computed: {
                total() {
                    let prices = 0
                    let filterCommodity = this.commodity.filter((item)=>item.checked)
                    filterCommodity.forEach((item)=>{
                        prices += item.price * item.count
                    })
                    return prices
                }
            }
        })
    </script>
</body>

</html>